package cn.ch.train.business.service;

import cn.ch.train.business.domain.DailyTrainSeat;
import cn.ch.train.business.domain.DailyTrainSeatExample;
import cn.ch.train.business.mapper.DailyTrainSeatMapper;
import cn.ch.train.business.request.DailyTrainSeatQueryParam;
import cn.ch.train.business.request.DailyTrainSeatSaveParam;
import cn.ch.train.business.response.DailyTrainSeatQueryResult;
import cn.ch.train.common.response.PageResult;
import cn.ch.train.common.util.SnowUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class DailyTrainSeatService {
    private static final Logger logger = LoggerFactory.getLogger(DailyTrainSeatService.class);
    @Resource
    private DailyTrainSeatMapper dailyTrainSeatMapper;

    public void save(DailyTrainSeatSaveParam param) {
        DateTime now = DateTime.now();
        DailyTrainSeat dailyTrainSeat = BeanUtil.copyProperties(param, DailyTrainSeat.class);
        if (Objects.isNull(dailyTrainSeat.getId())) {
            dailyTrainSeat.setId(SnowUtil.getSnowFlakeNextId());
            dailyTrainSeat.setCreateTime(now);
            dailyTrainSeat.setUpdateTime(now);
            dailyTrainSeatMapper.insert(dailyTrainSeat);
        } else {
            dailyTrainSeat.setUpdateTime(now);
            dailyTrainSeatMapper.updateByPrimaryKey(dailyTrainSeat);
        }
    }

    public PageResult<DailyTrainSeatQueryResult> queryList(DailyTrainSeatQueryParam param) {
        DailyTrainSeatExample dailyTrainSeatExample = new DailyTrainSeatExample();
        dailyTrainSeatExample.setOrderByClause("train_code asc, carriage_index asc, row asc, col asc");
        DailyTrainSeatExample.Criteria criteria = dailyTrainSeatExample.createCriteria();
        if (ObjectUtil.isNotEmpty(param.getTrainCode())) {
            criteria.andTrainCodeEqualTo(param.getTrainCode());
        }
        PageHelper.startPage(param.getPageNo(), param.getPageSize());
        List<DailyTrainSeat> dailyTrainSeatList = dailyTrainSeatMapper.selectByExample(dailyTrainSeatExample);

        PageInfo<DailyTrainSeat> dailyTrainSeatPageInfo = new PageInfo<>(dailyTrainSeatList);
        logger.info("总行数：{}", dailyTrainSeatPageInfo.getTotal());
        logger.info("总页数：{}", dailyTrainSeatPageInfo.getPages());
        List<DailyTrainSeatQueryResult> dailyTrainSeatQueryList = BeanUtil.copyToList(dailyTrainSeatList, DailyTrainSeatQueryResult.class);
        PageResult<DailyTrainSeatQueryResult> pageResult = new PageResult<>();
        pageResult.setTotal(dailyTrainSeatPageInfo.getTotal());
        pageResult.setList(dailyTrainSeatQueryList);
        return pageResult;
    }

    public void delete(Long id) {
        dailyTrainSeatMapper.deleteByPrimaryKey(id);
    }
}
